package com.sky.controller.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Consumer;

/**
 * @program: sky-take-out
 * @description:
 * @author: fmp
 * @create: 2023-04-09 15:10
 * @Version 1.0
 **/
@Slf4j
@Component
public class OrdersTask {
    @Resource
    private OrderMapper orderMapper;

    //支付订单超时处理
    @Scheduled(cron = "0 * * * * ? ")
    public void ordersparperPayTask(){
        LocalDateTime localDateTime = LocalDateTime.now().minusMinutes(15);
        List<Orders> ordersList =orderMapper.selectOrderByStatus(Orders.PENDING_PAYMENT,localDateTime);
        ordersList.forEach(orders -> {
            orders.setStatus(Orders.CANCELLED);
            orders.setCancelTime(LocalDateTime.now());
            orders.setCancelReason("订单超时~");
            orderMapper.updateOrder(orders);
        });
    }
    //派送订单
    @Scheduled(cron = "0 0 1 * * ?")
    public void ordersdeliveryTask(){
        LocalDateTime localDateTime =LocalDateTime.now().minusHours(1);
        List<Orders> ordersList =orderMapper.selectOrderByStatus(Orders.DELIVERY_IN_PROGRESS,localDateTime);
        ordersList.forEach(orders -> {
            orders.setStatus(Orders.COMPLETED);
            orders.setDeliveryTime(LocalDateTime.now());
            orderMapper.updateOrder(orders);
        });
    }
}
